//
//  ImagePicker.m
//  ImagePicker
//
//  Created by csh on 16/4/14.
//  Copyright © 2016年 com.read. All rights reserved.
//

#import "ImagePicker.h"

@interface ImagePicker ()<DVImagePickerNavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic,weak)id responder;
@property (nonatomic,strong)NSMutableArray *images;
@property (nonatomic,strong)NSMutableArray *selectArray;
@end

@implementation ImagePicker
- (instancetype)initWithDelegate:(__kindof UIViewController *)delegate
{
    self = [super init];
    if(self)
    {
        self.responder = delegate;
    }
    return self;
}


#pragma - mark show
- (void)show
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"from album" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        DVImagePickerNavigationController *navigationController = [[DVImagePickerNavigationController alloc] initWithArray:self.selectArray];
        navigationController.responder = self;
        [self.responder presentViewController:navigationController animated:YES completion:nil];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        [self.responder presentViewController:imagePicker animated:YES completion:nil];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [self.responder presentViewController:alert animated:YES completion:nil];
}


#pragma - mark delegateMethod
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    [[DVImagePickerManager manager] insertImage:image withCompletion:^(id result) {
        DVAssetModel *model = [[DVAssetModel alloc]init];
        model.asset = result;
        [self.selectArray addObject:model];
    }];
    if(self.images==nil)
    {
        self.images = [[NSMutableArray alloc]init];
    }
    [self.images addObject:image];
    if([self.responder respondsToSelector:@selector(didChooseImages:)])
    {
        [self.responder didChooseImages:self.images];
    }
    [self.responder dismissViewControllerAnimated:YES completion:nil];
}

#pragma - mark Delegate
- (void)didChooseImages:(NSMutableArray *)images assets:(NSMutableArray *)assets
{
    self.images = [[NSMutableArray alloc]init];
    [self.images addObjectsFromArray:images];
    self.selectArray = assets;
    if([self.responder respondsToSelector:@selector(didChooseImages:)])
    {
        [self.responder didChooseImages:self.images];
    }
}
@end
